import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';

import { CLIENT_DICT } from '#/api';
import { $t } from '#/locales';

/**
 * 查询参数
 */
export function querySchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('user.name'),
    },
    {
      component: 'DatePicker',
      componentProps: {
        rangeSeparator: '-',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        type: 'daterange',
      },
      fieldName: 'dateRange',
      label: $t('common.createTime'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        clearable: true,
        options: [
          {
            label: $t('common.success'),
            value: '0',
          },
          {
            label: $t('common.fail'),
            value: '1',
          },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function columnsSchema<T>(): VxeTableGridOptions<T>['columns'] {
  return [
    { type: 'checkbox', width: 50 },
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'username',
      title: $t('user.name'),
    },
    {
      field: 'loginIp',
      title: 'ip',
    },
    {
      field: 'loginLocation',
      title: $t('common.address'),
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          {
            label: $t('common.success'),
            value: '0',
          },
          {
            label: $t('common.fail'),
            value: '1',
            type: 'danger',
          },
        ],
      },
      field: 'status',
      title: $t('common.status'),
    },
    {
      cellRender: {
        name: 'CellTag',
        options: [
          {
            label: $t('client.grantType.refresh_token'),
            value: CLIENT_DICT.GRANT_TYPE_REFRESH,
          },
          {
            label: $t('client.grantType.password'),
            value: CLIENT_DICT.GRANT_TYPE_PASSWORD,
          },
          {
            label: $t('client.grantType.sms'),
            value: CLIENT_DICT.GRANT_TYPE_SMS,
          },
          {
            label: $t('client.grantType.client'),
            value: CLIENT_DICT.GRANT_TYPE_CLIENT,
          },
          {
            label: $t('client.grantType.wechat'),
            value: CLIENT_DICT.GRANT_TYPE_WECHAT,
          },
          {
            label: $t('client.grantType.device'),
            value: CLIENT_DICT.GRANT_TYPE_DEVICE,
          },
        ],
      },
      field: 'grantType',
      title: $t('client.authorizationGrantTypes'),
    },
    {
      field: 'errorMsg',
      title: $t('common.error') + $t('common.info'),
    },
    {
      field: 'loginTime',
      title: $t('common.createTime'),
    },
    {
      field: 'browser',
      title: $t('common.browser'),
    },
    {
      field: 'os',
      title: $t('common.os'),
    },
  ];
}
